<script setup lang="ts">
import { onMounted } from 'vue'
import { useCategoryStore } from '@/store/category.ts'
import { storeToRefs } from 'pinia'

defineProps({
  screen: {
    type: Boolean,
    default: false
  }
})

const { form, category1List, category2List, category3List } = storeToRefs(
  useCategoryStore(),
)
const { getCategory1Fn, getCategory2Fn, getCategory3Fn, getAttrInfoFn } = useCategoryStore()

onMounted(() => getCategory1Fn())

/**
 * 切换下拉框事件
 * type：切换的是几级分类
 * e：对应id
 */
const changeSelectFn = (type: string, e: number) => {
  switch (type) {
    case '1':
      getCategory2Fn(e)
      break
    case '2':
      getCategory3Fn(e)
      break
    case '3':
      getAttrInfoFn()
      break
    default:
      break
  }
}
</script>

<template>
  <el-card>
    <el-form :inline="true">
      <el-form-item label="一级分类">
        <el-select
          v-model="form.category1Id"
          :disabled="screen"
          @change="($event: number) => changeSelectFn('1', $event)"
        >
          <el-option
            v-for="item in category1List"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          v-model="form.category2Id"
          :disabled="screen"
          @change="($event: number) => changeSelectFn('2', $event)"
        >
          <el-option
            v-for="item in category2List"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          v-model="form.category3Id"
          :disabled="screen"
          @change="($event: number) => changeSelectFn('3', $event)"
        >
          <el-option
            v-for="item in category3List"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<style scoped></style>
